/**
 *******************************************************************************
 * @file  hc32_system.c
 * @brief This file provides firmware functions to manage the HC32 System.
 @verbatim
   Change Logs:
   Date             Author          Notes
   2019-06-26       chengy          First version
 @endverbatim
 *******************************************************************************
 * Copyright (C) 2016, Huada Semiconductor Co., Ltd. All rights reserved.
 *
 * This software is owned and published by:
 * Huada Semiconductor Co., Ltd. ("HDSC").
 *
 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
 *
 * This software contains source code for use with HDSC
 * components. This software is licensed by HDSC to be adapted only
 * for use in systems utilizing HDSC components. HDSC shall not be
 * responsible for misuse or illegal use of this software for devices not
 * supported herein. HDSC is providing this software "AS IS" and will
 * not be responsible for issues arising from incorrect user implementation
 * of the software.
 *
 * Disclaimer:
 * HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
 * REGARDING THE SOFTWARE (INCLUDING ANY ACCOMPANYING WRITTEN MATERIALS),
 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
 * WARRANTY OF NONINFRINGEMENT.
 * HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
 * SAVINGS OR PROFITS,
 * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
 * FROM, THE SOFTWARE.
 *
 * This software may be replicated in part or whole for the licensed use,
 * with the restriction that this Disclaimer and Copyright notice must be
 * included with each copy of this software, whether used in part or whole,
 * at all times.
 *******************************************************************************
 */

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "hc32_system.h"
#include "flash.h"

/**
 * @addtogroup CMSIS
 * @{
 */

/**
 * @addtogroup HC32_System
 * @{
 */

/*******************************************************************************
 * Global pre-processor symbols/macros ('define')
 ******************************************************************************/
/*!< Uncomment the following line if you need to relocate your vector Table in
     Internal SRAM. */
/* #define VECT_TAB_SRAM */
#define VECT_TAB_OFFSET                 (0x0UL) /*!< Vector Table base offset field.
                                                     This value must be a multiple of @ref VECTOR_TABLE_STEP. */

/**
 * @brief ICG Start Address
 */
#if defined (HC32F460) || defined (HC32F4A0)
#define ICG_START_ADDR                  0x400
#define ICG_START_ADDR_AC6              ".ARM.__at_0x400"
#elif defined (HC32M423)
#define ICG_START_ADDR                  0x240
#define ICG_START_ADDR_AC6              ".ARM.__at_0x240"
#elif defined (HC32F120) || defined (HC32M120) || defined (HC32F160)
#define ICG_START_ADDR                  0xC0
#define ICG_START_ADDR_AC6              ".ARM.__at_0xC0"
#endif

/*******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/*******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/
/**
 * @brief ICG parameters configuration
 */
#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
const uint32_t u32ICGValue[] __attribute__((section(ICG_START_ADDR_AC6))) =
#elif defined (__GNUC__) && !defined (__CC_ARM)
const uint32_t u32ICGValue[] __attribute__((section(".icg_sec"))) =
#elif defined (__CC_ARM)
const uint32_t u32ICGValue[] __attribute__((at(ICG_START_ADDR))) =
#elif defined (__ICCARM__)
#pragma location = ICG_START_ADDR
__root const uint32_t u32ICGValue[] =
#else
#error "unsupported compiler!!"
#endif
{
#if defined (HC32F460)
    /* ICG 0~1 */
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,   // HRC=16Mhz
    /* Reserved 2~7 */
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
#elif defined (HC32F4A0)
    /* ICG 0~3 */
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,   // HRC=16Mhz
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    /* Reserved 0~3 */
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    /* Reserved 4~7 */
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    /* Reserved 8~11 */
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    /* Reserved 12~15 */
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    /* Reserved 16~19 */
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
#elif defined (HC32M423)
    /* ICG 0~1 */
    0xFFE8FFFFUL,   // HRC=48Mhz
    0xFFFFFFFFUL,
    /* Reserved 2~6 */
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    /* ICG 7 */
    0xFFFFFFFFUL,
#elif defined (HC32F120) || defined (HC32M120) || defined (HC32F160)
    /* ICG 0~1 */
    0xFFF8FFFFUL,   // HRC=48Mhz
    0xFFFFFFFFUL,
    /* Reserved 2~6 */
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
    0xFFFFFFFFUL,
#endif
};

/*******************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/
/**
 * @addtogroup HC32_System_Global_Functions
 * @{
 */

/**
 * @brief  Setup the micro controller system.
 * @param  None
 * @retval None
 */
void SystemInit(void)
{
    /* Disable the RAM parity */
#if defined(HC32F120) || defined(HC32M120)
    M0P_PWC->FPRC  = 0xA5FFU;
    M0P_PWC->RAMCR |= PWC_RAMCR_RPERDIS;
    M0P_PWC->FPRC  = 0xA500U;
#endif

    /* Peripheral RAM low power control */
#if defined(HC32F4A0)
//    M4_PWC->FPRC    = 0xA50FU;
//    M4_PWC->PRAMLPC = 0x00040000UL;
//    M4_PWC->FPRC    = 0xA500U;
#endif

    /* Configure the Vector Table relocation */
#ifdef VECT_TAB_SRAM
    SCB->VTOR = SRAM_BASE  | VECT_TAB_OFFSET;   /* Vector Table Relocation in Internal SRAM. */
#else
    SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;   /* Vector Table Relocation in Internal FLASH. */
#endif
}

/**
 * @}
 */

/**
 * @}
 */

/**
 * @}
 */

/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/




